<template>
	<view class="parent-view">
		<view class="item-view">
			<text class="item-txt">姓名</text>
			<input class="item-input" placeholder="请输入收货人姓名" :value="name" @input="inputName"/>
		</view>
		<view class="item-view">
			<text class="item-txt">手机号</text>
			<input class="item-input" placeholder="请输入收货人电话" type="number" maxlength="11" :value="phone" @input="inputPhone"/>
		</view>
		<view class="item-view" @click="chooseAddress">
			<text class="item-txt">所在地区</text>
			<input class="item-input" placeholder="请选择收货人地区" disabled="false" :value="area" @input="inputArea"/>
		</view>
		<view class="item-textarea-view" style="margin-top: 5px;height: 94px;">
			<text class="item-txt" style="margin-top: 25px;">详细地址</text>
			<textarea class="item-textarea" placeholder="请输入收货人详细地址" :value="address" @input="inputAddress"/>
		</view>
		<view class="item-view"  style="margin-top: 5px;" @click="setDefault">
			<text class="item-txt" style="flex: 1;">设为默认地址</text>
			<image class="select-icon" :src="isDefault==1?'../../static/icon-complete-info-select.png'
					:'../../static/icon-complete-info-unselect.png'" />
		</view>
		
		<view class="save-btn" @click="setMallAddress">保存</view>
		<choseAddress :isShow="isShowChoseAddress" @returnAddress="addressReturn" />
	</view>
</template>

<script>
	import request from '../index/request.js';
	import choseAddress from '../../components/chose-address/chose-address.vue'
	export default {
		components: {
			choseAddress,
		},
		data() {
			return {
				isDefault:0,
				name:"",
				phone:"",
				area:"",
				address:"",
				addressId:"",
				isShowChoseAddress:false,
			}
		},
		methods: {
			onLoad: function(options) {
				this.appUtil.saveAppInviteId(options)
				if(options.data){
					var data=JSON.parse(decodeURIComponent(options.data))
					this.isDefault=data.isDefault
					this.name=data.name
					this.phone=data.phone
					this.area=data.address
					this.address=data.detail
					this.addressId=data.id
				}
			},
			onShow:function(){
				this.share.url='/pages/tabBar/home/index?appInviteId='+this.appUtil.getAppInviteId()
				this.share.shareType=1
			},
			inputName:function(e){
				this.name = e.target.value
			},
			inputPhone:function(e){
				this.phone = e.target.value
			},
			inputArea:function(e){
				this.area = e.target.value
			},
			inputAddress:function(e){
				this.address = e.target.value
			},
			setMallAddress:function(){
				if(!this.name){
					uni.showToast({
						title:"请输入收货人姓名",
						icon:"none"
					})
					return
				}
				if(!this.phone){
					uni.showToast({
						title:"请输入收货人电话",
						icon:"none"
					})
					return
				}
				if(this.phone.length!==11){
					uni.showToast({
						title:"收货人电话输入有误",
						icon:"none"
					})
					return
				}
				if(!this.area){
					uni.showToast({
						title:"请选择收货人地区",
						icon:"none"
					})
					return
				}
				if(!this.address){
					uni.showToast({
						title:"请输入收货人详细地址",
						icon:"none"
					})
					return
				}
				uni.showLoading()
				request.post("api/mall/set_mall_address", {
					name:this.name,
					phone:this.phone,
					address:this.area,
					detail:this.address,
					isDefault:this.isDefault,
					id:this.addressId
				}).then(data => {
					uni.navigateBack()
				})
			},
			chooseAddress:function(){
				this.isShowChoseAddress=true
			},
			addressReturn: function(event) {
				this.isShowChoseAddress = event.isclose
				if (event.tag) {
					this.area = event.address
				}
			},
			setDefault:function(){
				if(this.isDefault==1){
					this.isDefault=0
				}else{
					this.isDefault=1
				}
			}
		}
	}
</script>

<style>
	@import "index.css";
</style>
